package com.quectel.mobile.controller.forum;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.constant.core.forum.ForumConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.forum.dto.mongo.ForumMsgDto;
import com.quectel.core.module.forum.service.mongo.ForumMsgService;
import com.quectel.mobile.controller.BaseController;
import com.quectel.mobile.util.SessionHolder;
import com.quectel.mobile.vo.forum.UpdateReadStatusReq;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/5/15 9:24
 * @description new java file header...
 */
@RestController
@RequestMapping("forumMsg")
@Tag(name = "ForumMsgController", description = "发帖消息相关api")
public class ForumMsgController extends BaseController {
    private final Byte POST = 1;
    private final Byte LIKE = 2;
    private final Byte GET_LIKE = 3;
    private final Byte COMMENT = 4;
    private final Byte REPLY_COMMENT = 5;

    @DubboReference
    private ForumMsgService forumMsgService;

    @PostMapping("batchUpdateReadStatus")
    @Operation(summary = "批量更新未读状态")
    public Response<Byte> batchUpdateReadStatus(
            @RequestBody UpdateReadStatusReq updateReadStatusReq
    ) {
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        request.put(LambdaUtil.getFieldName(ForumMsgDto::getMobileUserId), SessionHolder.getLoginMobileUserSession().getMobileUser().getId());

        handleMsgType(request, updateReadStatusReq.getMsgType());

        //更新未读消息
        request.put(LambdaUtil.getFieldName(ForumMsgDto::getReadStatus), SystemConstants.NO);

        forumMsgService.batchUpdateReadStatus(request.getParams());

        return Response.ok();
    }

    @PostMapping("updateReadStatus")
    @Operation(summary = "更新未读状态")
    public Response<Byte> updateReadStatus(
            @RequestBody Long msgId
    ) {
        ForumMsgDto forumMsgDto = forumMsgService.selectById(msgId);
        if (forumMsgDto == null) {
            return Response.error("消息不存在");
        }
        forumMsgDto.setReadStatus(SystemConstants.YES);
        forumMsgService.updateById(forumMsgDto);
        return Response.ok();
    }


    @GetMapping("unreadMsgStatus")
    @Operation(summary = "unreadMsgStatus")
    public Response<Byte> myMsg(
            @Parameter(description = "消息类型1发帖2点赞3获赞4评论5回复") @RequestParam(required = false) Byte msgType
    ) {
        Request request = Request.configParams(Request.parseParams(httpServletRequest));
        //只需要查询一条未读就行
        request.put("pageSize", 1);
        request.put("pageNo", 1);

        request.put(LambdaUtil.getFieldName(ForumMsgDto::getMobileUserId), SessionHolder.getLoginMobileUserSession().getMobileUser().getId());
        request.put(LambdaUtil.getFieldName(ForumMsgDto::getReadStatus), SystemConstants.NO);

        handleMsgType(request, msgType);

        List<ForumMsgDto> forumMsgDtos = forumMsgService.queryList(request.getParams());
        if (CollectionUtils.isNotEmpty(forumMsgDtos)) {
            return Response.<Byte>ok().wrap(SystemConstants.YES);
        } else {
            return Response.<Byte>ok().wrap(SystemConstants.NO);
        }
    }

    @GetMapping("myMsg")
    @Operation(summary = "我的消息")
    public Response<Page<ForumMsgDto>> myMsg(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize,
            @Parameter(description = "消息类型1发帖2点赞3获赞4评论5回复") @RequestParam Byte msgType
    ) {
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        request.put(LambdaUtil.getFieldName(ForumMsgDto::getMobileUserId), SessionHolder.getLoginMobileUserSession().getMobileUser().getId());

        handleMsgType(request, msgType);

        List<ForumMsgDto> forumMsgDtos = forumMsgService.queryList(request.getParams());

        Page<ForumMsgDto> page = new Page<>(request.getPageNo(), request.getPageSize(), 0, forumMsgDtos);
        return Response.<Page<ForumMsgDto>>ok().wrap(page);
    }

    private Request handleMsgType(Request request, Byte msgType) {

        if (POST.equals(msgType)) {
            request.put(ForumConstants.MSG_TYPES, new Byte[]{ForumConstants.ForumHandleType.POST.getType()});
        } else if (LIKE.equals(msgType)) {
            //点赞的操作人是当前用户
            request.put(LambdaUtil.getFieldName(ForumMsgDto::getOperatorId), SessionHolder.getLoginMobileUserSession().getMobileUser().getId());
            request.put(ForumConstants.MSG_TYPES, new Byte[]{ForumConstants.ForumHandleType.LIKE.getType(), ForumConstants.ForumHandleType.LIKE_COMMENT.getType()});
        } else if (GET_LIKE.equals(msgType)) {
            //获赞的操作人是对方
            request.put(LambdaUtil.getFieldName(ForumMsgDto::getOperatedPersonId), SessionHolder.getLoginMobileUserSession().getMobileUser().getId());
            request.put(ForumConstants.MSG_TYPES, new Byte[]{ForumConstants.ForumHandleType.LIKE.getType(), ForumConstants.ForumHandleType.LIKE_COMMENT.getType()});
        } else if (COMMENT.equals(msgType)) {
            //评论,发起人是自己
            request.put(LambdaUtil.getFieldName(ForumMsgDto::getOperatorId), SessionHolder.getLoginMobileUserSession().getMobileUser().getId());
            request.put(ForumConstants.MSG_TYPES, new Byte[]{ForumConstants.ForumHandleType.COMMENT_COMMENT.getType(), ForumConstants.ForumHandleType.POST_COMMENT.getType()});
        } else if (REPLY_COMMENT.equals(msgType)) {
            //回复的的评论,发起人是对方
            request.put(LambdaUtil.getFieldName(ForumMsgDto::getOperatedPersonId), SessionHolder.getLoginMobileUserSession().getMobileUser().getId());
            request.put(ForumConstants.MSG_TYPES, new Byte[]{ForumConstants.ForumHandleType.COMMENT_COMMENT.getType(), ForumConstants.ForumHandleType.POST_COMMENT.getType()});
        }
        return request;
    }


}
